package com.ktjy.crm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.crm.mapper.UserMapper;
import com.ktjy.crm.model.User;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.springframework.test.util.AssertionErrors.assertEquals;

@SpringBootTest
public class UserMapperTester {
    @Resource // 自动注入
    private UserMapper userMapper;

    @Test
    void testInsert1() {
        User user = new User();
        user.setUsrName("ktjiaoyu");  // 使用修正后的属性名
        user.setUsrPassword("12356");
        user.setUsrRoleId(2L);
        user.setUsrFlag(1);
      int rows =  userMapper.inserts(user);
        System.out.println("rows:"+rows);
    }

    @Test
    public void testDelete() throws Exception {
        userMapper.delete(7L);
    }
    @Test
    public void testUpdate1() throws Exception {
        User user = new User();
        user.setUsrId(6L);
        user.setUsrName("ktjiaoyu1");
        user.setUsrPassword("12356");
        userMapper.update(user);
    }
    @Test
    public void testGet() throws Exception {
        User user = userMapper.get(2L);
        System.out.println(user);
    }
    @Test
    public void testFindAll1() throws Exception {
        System.out.println(userMapper.findAll());
    }

    @Test
    public void testSelectById(){
        User user=userMapper.selectById(2L);
        System.out.println("usrName"+user.getUsrName());
    }

    @Test
    public void testInsert2(){// 测试插入数据2
        userMapper.insert(new User("MyBatis-Plus", "123456", 1L, 1));
    }

    @Test
    public void testFindAll2(){
        List<User> list=userMapper.selectList(null);
        for (User user : list) {
            System.out.println("UsrName"+user.getUsrName());
        }
    }

    @Test
    public void testFind(){//条件查询根据用户名和密码查询
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.eq("usr_name","MyBatis-Plus");
        wrapper.eq("usr_password","123456");
        List<User>list=userMapper.selectList(wrapper);
        if (list!=null){
            for (User user : list) {
                System.out.println("UsrId="+user.getUsrId());
            }
        }
    }

    @Test
    public void testGetUserByUserNameWithRoleName(){
        User user=userMapper.getUserByUserNameWithRoleName("admin");
        if (user!=null){
            System.out.println("usrId="+user.getUsrId()+"roleName="+user.getRoleName());
        }
    }

    @Test
    public void testPagination(){ //分页查询
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("usr_role_id",2L);
        //分页对象 查询获得User对象 ,查看第1页数据 每页2条数据
        Page<User> page=new Page<>(1,2);
        IPage<User> iPage=userMapper.selectPage(page,queryWrapper);
        System.out.println("总页数-------------->"+page.getPages());
        System.out.println("总记录数-------------->"+page.getTotal());
        System.out.println("当前页数据数量-------------->"+iPage.getRecords().size());
        System.out.println("每页记录数-------------->"+page.getSize());
        System.out.println("当前记录---------->");
        for (User user : iPage.getRecords()){//获取查询记录
            System.out.println("usrId:"+user.getUsrId());
        }
    }
    //更新操作
    @Test
    public void testUpdateAndVersuin(){
        User user=userMapper.selectById(7L); //读取用户数据
        user.setUsrPassword("1234567");
        user.setUsrRoleId(1L);

        int result=userMapper.updateById(user);//更新用户数据
        if (result==0){
            //更新失败 版本号不匹配
            System.out.println("更新失败 ：数据可能已经被其他用户修改");
        }else {
            //更新成功 MyBatis会自动将version字段+1
            System.out.println("更新成功 ："+user);
        }
    }
    //测试防全全表更新与删除插件
    @Test
    public void testFullUpdate(){
        User user=new User();
        user.setUsrId(16L);
        user.setUsrPassword("12345678");
        userMapper.update(user);
    }
    //测试逻辑删除
    @Test
    public void testDeleteByLogic(){
        userMapper.deleteById(7L);
    }
    //测试逻辑查询
    @Test
    public void testFindAll(){
        List<User> list=userMapper.selectList( null);
        for (User user : list){
            System.out.println("usrName:"+user.getUsrName());
        }
    }
    @Test
    public void testUpdate2(){
        User user=new User();
        user.setUsrPassword("1111111111");
        user.setUsrId(16L);

        userMapper.updateById(user);
    }
}